package com.iwinding.ladygg.ui.act;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;

import com.astuetz.PagerSlidingTabStrip;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.ui.adapter.FragmentPagerAdapter;
import com.iwinding.ladygg.ui.fragment.MyHintFragment;
import com.iwinding.ladygg.ui.fragment.MySosRecordFragment;


/**
 * Created by hezhiyong on 2017/3/14.
 */

public class SosRecordActivity extends BaseFragmentActivity implements ViewPager.OnPageChangeListener{

    private PagerSlidingTabStrip tabs;
    private ViewPager pager;
    private MyPagerAdapter adapter;

    private MySosRecordFragment mySosRecordFragment;
    private MyHintFragment myHintFragment;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected int getLayoutID() {
        return R.layout.icane_activity_sos_record;
    }


    void initView(){
        setTitleResId(R.string.icane_sos_record);
        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        pager = (ViewPager) findViewById(R.id.pager);
        adapter = new MyPagerAdapter(getSupportFragmentManager());

        pager.setAdapter(adapter);
        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                .getDisplayMetrics());
        pager.setPageMargin(pageMargin);

        tabs.setViewPager(pager);

        tabs.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        pager.setCurrentItem(position);
        switch (position){
            case 0:
                break;
            case 1:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        private final int[] TITLES = { R.string.icane_my_sos, R.string.icane_my_hint };

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return getString(TITLES[position]);
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position){
                case 0:
                    if(mySosRecordFragment==null){
                        mySosRecordFragment = new MySosRecordFragment();
                    }
                    fragment =mySosRecordFragment;
                    break;
                case 1:
                    if(myHintFragment==null){
                        myHintFragment = new MyHintFragment();
                    }
                    fragment = myHintFragment;
                    break;
            }
            return fragment;
        }

    }

}
